package com.zhenhe.core.common.utils.pay.bohai;

import java.math.BigDecimal;
import java.text.DecimalFormat;
import java.text.NumberFormat;
import java.util.HashMap;
import java.util.Map;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

//import com.zhenhe.core.common.global.Global;
import com.zhenhe.core.common.utils.pay.bohai.support.PacUtil;

/**
 * 渤海银行支付接口
 * @author hl
 *
 */
public class BoHaiUtil {

	public static final Logger log = LoggerFactory.getLogger("pay.bohai");
	
	/**
	 * 
	 * @param pyeAct 收款人账号
	 * @param pyeNam 收款人姓名
	 * @param pyeBnk 收款人银行
	 * @param amt 金额 
	 * @example 330201100120  测试账户 402731057238 1.01
	 * @return
	 */
	public static boolean bohaiPay(String pyeAct,String pyeNam,String pyeBnk,BigDecimal amt){
		String transType = "SCSP";//SCSP or BIQ or SCTQ

		NumberFormat nf = new DecimalFormat(".00");
		
        Map<String, Object> dataMap = new HashMap<>();
        dataMap.put("transType", transType);

        dataMap.put("instId", "100026");
        dataMap.put("certId", "1000262016032501");
        dataMap.put("pyrAct", "2001841274000166");
        dataMap.put("pyrNam", "2001841274");
        dataMap.put("instSeq", "" + System.currentTimeMillis());
        dataMap.put("pyeAct", pyeAct);
        dataMap.put("pyeNam", pyeNam);
        dataMap.put("pyeBnk", pyeBnk);
        dataMap.put("amt", nf.format(amt.doubleValue()));
        dataMap.put("postscript", "TEST用途");
        
       return  bohaiPay(dataMap);
	}
	
	private static boolean bohaiPay(Map<String, Object> dataMap){
		
		String transType = (String) dataMap.get("transType");

		BoHaiHelp bhh = new BoHaiHelp();//Global.getBoHaiHelp();
		
        String request = bhh.sign(PacUtil.formatData(dataMap), transType);

        String response;
		try {
			response = PacUtil.issuePac(request);
		} catch (Exception e) {
			log.warn("支付出错",e);
			e.printStackTrace();
			return false;
		}

        log.info("Trans response:" + response);
        
        dataMap.put("response", response);

        boolean verifyFlag = true;

//        if (null != response) {
//            verifyFlag = bhh.verify(response);
//            log.info("Verify result:"+verifyFlag);
//        }
        
        
        String rspCode = null;
        String rspMsg = "";

        if (verifyFlag) {

            String errorCode = null;
            String errorMessage = "";

            int pos1 = -1;
            int pos2 = -1;
            pos1 = response.indexOf("<rspCode>");
            if (pos1 != -1) {
                pos2 = response.indexOf("</rspCode>");
                rspCode = response.substring(pos1 + 9, pos2);
            }

            pos1 = response.indexOf("<rspMsg>");
            if (pos1 != -1) {
                pos2 = response.indexOf("</rspMsg>");
                rspMsg = response.substring(pos1 + 8, pos2);
            }

            if (null == rspCode) {
                rspCode = "9";
            }

            dataMap.put("rspCode", rspCode);
            dataMap.put("rspMsg", rspMsg);

            pos1 = response.indexOf("<errorCode>");
            if (pos1 != -1) {
                pos2 = response.indexOf("</errorCode>");
                errorCode = response.substring(pos1 + 11, pos2);
            }

            pos1 = response.indexOf("<errorMessage>");
            if (pos1 != -1) {
                pos2 = response.indexOf("</errorMessage>");
                errorMessage = response.substring(pos1 + 14, pos2);
            }

            if (null != errorCode) {
                dataMap.put("errorCode", errorCode);
                dataMap.put("errorMessage", errorMessage);
            }
        } else {
            dataMap.put("errorCode", "9999");
            dataMap.put("errorMessage", "验证服务端签名失败");
            return false;
        }
        return "0".equals(dataMap.get("rspCode"));
	}
	
	public static void main(String[] args) {
//		String transType = "SCSP";//SCSP or BIQ or SCTQ
//
//        Map<String, Object> dataMap = new HashMap<>();
//        dataMap.put("transType", transType);
//
//        dataMap.put("instId", "100099");
//        dataMap.put("certId", "1000992014081101");
//        dataMap.put("pyrAct", "2000713325000168");
//        dataMap.put("pyrNam", "2000713325");
//        dataMap.put("instSeq", "" + System.currentTimeMillis());
//        dataMap.put("pyeAct", "330201100120");
//        dataMap.put("pyeNam", "测试账户");
//        dataMap.put("pyeBnk", "402731057238");
//        dataMap.put("amt", "1.01");
//        dataMap.put("postscript", "TEST用途");
//        
//        boolean b = bohaiPay(dataMap);
//        System.out.println(b);
		
//		System.out.println(bohaiPay("330201100120", "测试账户", " 402731057238", new BigDecimal(1.01)));
//		System.out.println(bohaiPay("6217920801580072", "胡良", "310305000013", new BigDecimal(1)));
	
		Map<String, Object> dataMap = new HashMap<>();
		dataMap.put("transType", "BIQ");

        dataMap.put("instId", "100026");
        dataMap.put("certId", "1000262016032501");
		
        bohaiPay(dataMap);
        
	}
	
}
